import sys
from PyQt5.QtWidgets import (
    QApplication,
    QMainWindow,
    QWidget,
    QVBoxLayout,
    QHBoxLayout,
    QLabel,
    QStackedWidget,
    QPushButton,
    QSizePolicy,
    QMessageBox,
)
from PyQt5.QtCore import Qt, QRect
from PyQt5.QtGui import QPainter, QColor, QBrush, QPen, QLinearGradient

# 引入外部视图
from view.dashboord import DashboardView, ExamView, ProfileView


class Home(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.setAttribute(Qt.WA_TranslucentBackground)
        self.setGeometry(0, 0, 1000, 600)
        self.center()

        central_widget = QWidget(self)
        central_widget.setStyleSheet("background: transparent;")  # 关键：设置透明背景
        self.setCentralWidget(central_widget)

        main_layout = QVBoxLayout(central_widget)
        main_layout.setContentsMargins(8, 8, 8, 8)  # 关键：留出8px边距给圆角
        main_layout.setSpacing(0)

        # 顶部导航栏
        nav_bar = QWidget()
        nav_bar.setFixedHeight(40)
        nav_bar.setStyleSheet(
            """
            background-color: rgba(255, 255, 255, 0.1);
            border-bottom: 1px solid rgba(0, 0, 0, 0.1);
        """
        )
        nav_layout = QHBoxLayout(nav_bar)
        nav_layout.setContentsMargins(25, 0, 10, 0)

        # 教师端标签
        teacher_label = QLabel("教师端")
        teacher_label.setStyleSheet(
            """
            font-size: 16px; 
            color: #333333; 
            background-color: transparent;
        """
        )
        nav_layout.addWidget(teacher_label)
        nav_layout.addStretch()

        # 关于按钮
        about_btn = QPushButton("关于")
        about_btn.setStyleSheet(
            """
            QPushButton {
                color: #333333;
                font-size: 14px;
                border: none;
                background-color: transparent;
                padding: 5px 10px;
            }
            QPushButton:hover {
                background-color: rgba(0, 0, 0, 0.1);
            }
        """
        )
        about_btn.clicked.connect(self.show_about)
        nav_layout.addWidget(about_btn)

        # 关闭按钮
        close_btn = QPushButton("×")
        close_btn.setStyleSheet(
            """
            QPushButton {
                color: #333333;
                font-size: 20px;
                border: none;
                background-color: transparent;
                padding: 0 10px;
            }
            QPushButton:hover {
                background-color: rgba(0, 0, 0, 0.1);
            }
        """
        )
        close_btn.clicked.connect(self.close_system)
        nav_layout.addWidget(close_btn)

        main_layout.addWidget(nav_bar)

        # 内容区域布局
        content_layout = QHBoxLayout()
        content_layout.setContentsMargins(0, 0, 0, 0)
        content_layout.setSpacing(0)

        # Left side panel (导航栏)
        left_panel = QWidget()
        left_panel.setFixedWidth(200)
        left_panel.setStyleSheet(
            """
            background-color: rgba(255, 255, 255, 0.3);
            border-right: 1px solid rgba(0, 0, 0, 0.1);
        """
        )
        left_layout = QVBoxLayout(left_panel)
        left_layout.setContentsMargins(0, 20, 0, 20)
        left_layout.setSpacing(0)

        # 导航栏项
        self.items = ["数据看板", "考试发布", "个人信息"]
        self.buttons = []
        for item in self.items:
            button = QPushButton(item)
            button.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
            button.setStyleSheet(
                """
                QPushButton {
                    color: #333333;
                    font-size: 14px;
                    text-align: left;
                    padding: 15px 25px;
                    border: none;
                    background-color: transparent;
                    margin: 0px;
                }
                QPushButton:hover {
                    background-color: rgba(255, 255, 255, 0.5);
                    border-radius: 0px;
                }
            """
            )
            button.clicked.connect(self.switch_view)
            left_layout.addWidget(button)
            self.buttons.append(button)

        left_layout.addStretch()
        content_layout.addWidget(left_panel)

        # Right side panel (功能区)
        self.right_panel = QStackedWidget()
        self.right_panel.setStyleSheet("background-color: transparent;")

        # 动态加载视图
        self.dashboard_view = DashboardView()
        self.exam_view = ExamView()
        self.profile_view = ProfileView()

        self.right_panel.addWidget(self.dashboard_view)
        self.right_panel.addWidget(self.exam_view)
        self.right_panel.addWidget(self.profile_view)

        # 默认显示数据看板视图
        self.right_panel.setCurrentIndex(0)
        self.buttons[0].setStyleSheet(
            """
            color: #333333;
            font-size: 14px;
            text-align: left;
            padding: 15px 25px;
            border: none;
            background-color: rgba(255, 255, 255, 0.5);
            border-radius: 0px;
        """
        )

        content_layout.addWidget(self.right_panel)
        main_layout.addLayout(content_layout)

    def center(self):
        screen = QApplication.primaryScreen().geometry()
        x = (screen.width() - self.width()) // 2
        y = (screen.height() - self.height()) // 2
        self.move(x, y)

    def paintEvent(self, event):
        painter = QPainter(self)
        painter.setRenderHint(QPainter.Antialiasing)

        # 使用减去边距的矩形
        rounded_rect = self.rect().adjusted(8, 8, -8, -8)  # 关键：向内缩进8px

        # 绘制背景渐变（填充整个区域）
        gradient = QLinearGradient(0, 0, self.width(), self.height())
        gradient.setColorAt(0, QColor(135, 206, 250))
        gradient.setColorAt(1, QColor(221, 160, 221))
        painter.setBrush(QBrush(gradient))
        painter.setPen(Qt.NoPen)
        painter.drawRoundedRect(rounded_rect, 8, 8)  # 在调整后的矩形上绘制圆角

        # 绘制顶部导航栏的装饰线（需要调整位置）
        pen = QPen(QColor(0, 0, 0, 25))
        pen.setWidth(1)
        painter.setPen(pen)
        painter.drawLine(8, 48, 208, 48)  # 水平线（y=40+8边距）
        painter.drawLine(208, 8, 208, 48)  # 垂直线（x=200+8边距）

    def switch_view(self):
        """切换视图"""
        button = self.sender()
        index = self.buttons.index(button)

        # 更新按钮样式
        for btn in self.buttons:
            btn.setStyleSheet("""
                QPushButton {
                    color: #333333;
                    font-size: 14px;
                    text-align: left;
                    padding: 15px 25px;
                    border: none;
                    background-color: transparent;
                    margin: 0px;
                }
                QPushButton:hover {
                    background-color: rgba(255, 255, 255, 0.5);
                    border-radius: 0px;
                }
            """)
        button.setStyleSheet(
            """
            color: #333333;
            font-size: 14px;
            text-align: left;
            padding: 15px 25px;
            border: none;
            background-color: rgba(255, 255, 255, 0.5);
            border-radius: 0px;
        """
        )

        # 切换视图
        self.right_panel.setCurrentIndex(index)

    def close_system(self):
        QApplication.quit()

    def show_about(self):
        QMessageBox.information(
            self,
            "关于我们",
            "版本信息：1.0.0\n\n开发团队：XXX技术部",
            QMessageBox.Ok,
        )

    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.dragPos = event.globalPos()

    def mouseMoveEvent(self, event):
        if event.buttons() == Qt.LeftButton:
            self.move(self.pos() + event.globalPos() - self.dragPos)
            self.dragPos = event.globalPos()
            event.accept()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Home()
    window.show()
    sys.exit(app.exec_())